from flask import Flask, Blueprint,request, jsonify
import sys
import  os
import json






# from  ..service.index_service_1  import  hello

from pathlib import Path
# 动态添加utils文件夹的路径
current_file_path = Path(os.path.abspath(__file__))
parent_dir = current_file_path.parent.parent
print(parent_dir)
sys.path.append(os.path.join(parent_dir, 'service'))
from  service  import  assistantLoginService


assistantLogin = Blueprint("assistantLogin", __name__)



@assistantLogin.route("/homePage", methods=['GET','POST'])
def assistantLogin_homePage():

    """
    :功能: 登录页面初始化接口
    :param name: 参数的注释信息
    :return: 函数的返回值
    """


    result = assistantLoginService.assistantLogin_homePage()

    print("   result   =  ")
    print(result)

    json_str = json.dumps(result, ensure_ascii=False)
    print("   json_str =  ")
    print(json_str)

    return json_str



@assistantLogin.route("/verification", methods=['GET','POST'])
def assistantLogin_verification():

    """
    :功能: 生成验证码接口
    :param name: 参数的注释信息
    :return: 函数的返回值
    """

    # all_headers = dict(request.headers)
    # 获取特定的请求头
    cacheTokenId = request.headers.get('cacheTokenId')
    userName = request.headers.get('userName')
    print("   cacheTokenId   =  ")
    print(cacheTokenId)

    print("   userName   =  ")
    print(userName)

    result = assistantLoginService.assistantLogin_verification(cacheTokenId,userName)

    print("   result   =  ")
    print(result)

    json_str = json.dumps(result, ensure_ascii=False)
    print("   json_str =  ")
    print(json_str)

    data = {
        "institutionName": "玉鸟随园嘉树养老社区",
        "userName": 'test',
        "loginPassWord": "123456#"
    }
    data_json = json.dumps(data, ensure_ascii=False)
    print("   data_json =  ")
    print(data_json)

    return json_str




@assistantLogin.route('/login', methods=['GET','POST'])
def assistantLogin_login():

    """
    :功能: 登录页面
    :userInfo : 登录用户信息
    :return: 成功返回 success    失败返回  false
    """
    # 获取 JSON 数据
    data = request.get_json()
    if not data:
        return jsonify({'error': 'No data provided'}), 400

    # 处理登录逻辑
    institutionId = data.get('institutionId')
    username = data.get('userName')
    password = data.get('loginPassWord')

    print("   institutionId =  ")
    print(institutionId)

    print("   username =  ")
    print(username)

    print("  password =  ")
    print(password)
    if  institutionId == None :
        data = {
            "result": 0,
            "reason": "机构名错误"
        }
        data_json = json.dumps(data, ensure_ascii=False)
        return  data_json

    if  username == None :
        data = {
            "result": 0,
            "reason": "账号错误"
        }
        data_json = json.dumps(data, ensure_ascii=False)
        return  data_json

    if  password == None :
        data = {
            "result": 0,
            "reason": "密码错误"
        }
        data_json = json.dumps(data, ensure_ascii=False)
        return  data_json

    result = assistantLoginService.assistantLogin_login(institutionId,username,password)

    print("   result   =  ")
    print(result)

    data = result
    data_json = json.dumps(data, ensure_ascii=False)

    return data_json






